/*
 * CSCI311App.java
 */
package csci311;

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import java.util.Vector;
import javax.swing.JTextField;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class CSCI311App extends SingleFrameApplication {

//    public static final String FILES_TO_INDEX_DIRECTORY = "filesToIndex";
//    public static final String INDEX_DIRECTORY = "indexDirectory";
//    public static final File INDEX_DIR = new File("index");
//    public static final String FIELD_PATH = "path";
//    public static final String FIELD_CONTENTS = "contents";
    public static final String FILES_TO_INDEX_DIRECTORY = "C:\\test\\filesToIndex"; //This is where to put the documents you want to crawler to search
    public static final File INDEX_DIR = new File("C:\\test\\indexDirectory"); //This is where the indexer will create the Lucene index files
    public static final File BACKUP_INDEX_DIR = new File("C:\\test\\backupIndexDirectory"); //This is where backup of the index is created
    public static final String FIELD_FILENAME = "filename";
    public static final String FIELD_PATH = "path";
    public static final String FIELD_CONTENTS = "contents";
    public static final boolean PERFORM_RECURSIVE_CRAWL = true;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        show(new CSCI311View(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of CSCI311App
     */
    public static CSCI311App getApplication() {
        return Application.getInstance(CSCI311App.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException {
        createIndex(); //Create the index
        launch(CSCI311App.class, args);
    }

    @Action
    public void runSearch() {
    }

    public static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException {
        Indexer indexer = new Indexer();
        indexer.createIndex(INDEX_DIR,BACKUP_INDEX_DIR, FILES_TO_INDEX_DIRECTORY, FIELD_PATH, FIELD_FILENAME, FIELD_CONTENTS, PERFORM_RECURSIVE_CRAWL);
    }

    public static ArrayList<ArrayList> runQuery(String queryText) throws ParseException, CorruptIndexException, IOException {
        return Searcher.runQuery(queryText, INDEX_DIR, FIELD_CONTENTS, FIELD_PATH, FIELD_FILENAME);
    }
}
